package com.a3dweijing.wetouch2.Activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.a3dweijing.wetouch2.Common.CallBack;
import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.AlerDialogHelp;
import com.a3dweijing.wetouch2.Util.EvenBusUtil;
import com.a3dweijing.wetouch2.Util.ProgressDialogHelp;
import com.a3dweijing.wetouch2.base.BaseActivity;
import com.a3dweijing.wetouch2.bean.EvenMsg;
import com.a3dweijing.wetouch2.bean.ResultMsg;
import com.a3dweijing.wetouch2.bean.User;
import com.a3dweijing.wetouch2.constant.Api;
import com.a3dweijing.wetouch2.datasave.Singleton;
import com.a3dweijing.wetouch2.network.RetrofitUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ResetNicknameActivity extends BaseActivity {
    public final static  int RESETNICKNAMEACTIVITYCODE = 56;

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.top_text)
    TextView topText;
    @BindView(R.id.top_right)
    TextView topRight;
    @BindView(R.id.top_all)
    RelativeLayout topAll;
    @BindView(R.id.ed_nickname)
    EditText edNickname;
    @BindView(R.id.save)
    Button save;
    private User user;
    private ProgressDialog dialog;
    private AlertDialog dialog1;
    private Api api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reset_nickname);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        user = Singleton.getInstance().getUser();
        topText.setText("修改昵称");
        edNickname.setText(user.getUser_name());
        dialog = ProgressDialogHelp.create(ResetNicknameActivity.this, "提示", "正在提交中", false);
        dialog1 = AlerDialogHelp.create(ResetNicknameActivity.this, "修改昵称", "是否确定修改姓名？", false, new CallBack() {
            @Override
            public void doNext(int what, Object obj) {
                // 请求
                final String name = edNickname.getText().toString().trim();
                int id = Integer.parseInt(Singleton.getInstance().getUser().getUser_id());
                api = RetrofitUtil.getInstance().getApiOfPhp();
                Observable<ResultMsg> cData = api.editUsername(id, name);
                cData.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(
                        new Observer<ResultMsg>() {
                            @Override
                            public void onSubscribe(Disposable d) {
                                dialog.show();
                            }

                            @Override
                            public void onNext(ResultMsg resultMsg) {
                                dialog.dismiss();
                                if (resultMsg.isFlag()) {
                                    user.setUser_name(name);
                                    Toast.makeText(ResetNicknameActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                    setResult(RESULT_OK);
                                    finish();
                                }
                            }

                            @Override
                            public void onError(Throwable e) {
                                dialog.dismiss();
                                Toast.makeText(ResetNicknameActivity.this, "修改失败", Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        });
    }


    @OnClick({R.id.back, R.id.save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.save:
                dialog1.show();
                break;
        }
    }
}
